<?php


namespace app\api\model;

use app\logic\model\user\UserLogicModel;
use think\facade\Db;

class UserModel extends UserLogicModel
{
    public function getSelect(array $where=[],string $field='*',string $order='id desc',int $limit=15){
        return $this->where($where)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
    }
    public function findUser($_where,$_field){
    	return Db::name("user")->where($_where)->field($_field)->find();
    }
    
    public function findDownUser($_where,$_field){
    	return Db::name("user")->where($_where)->field($_field)->find();
    }
    public function findUserInfo($_where,$_field){
    	return Db::name("user_info")->where($_where)->field($_field)->find();
    }
    
    public function selectDownUser($_where,$_field,$_limit,$_order){
    	$_data=Db::name("user")->where($_where)->field($_field)->limit(current($_limit),end($_limit))->order($_order)->select();
    	return !empty($_data)?$_data->toArray():[];
    }
    public function insertUser($_data){
    	return Db::name("user")->insert($_data,true);
    }
    public function insertInfo($_data){
    	return Db::name("user_info")->insert($_data,true);
    }
    
    public function editUserInfo($_up,$_where){
    	return Db::name("user_info")->where($_where)->update($_up);
    }
    
    public function updateInfo($_up,$_where){
    	return Db::name("user")->where($_where)->update($_up);
    }
    /**
     * 获取当前用户的上级（团员or团长）和上级的团长和对应的佣金
     */
    function getAgent(){
        global $user;
        $return=array(
            'member_uid'=>false,
            'head_uid'=>false,
        );
        if(!empty($user['agentid'])){
            $agentUser=$this->where('id',$user['agentid'])->find();
            if(!empty($agentUser)){
                if($agentUser['is_head']==1){//如果上级用户是团长的话，只返回团长
                    $return['head_uid']=$agentUser['id'];
                }else{
                    if($agentUser['is_member']==1){//如果上级用户是团员的话，查询团员的团长
                        $return['member_uid']=$agentUser['id'];
                        $return['head_uid']=$this->where('id',$agentUser['head_id'])->value('id');
                    }
                }
            }
        }
        return $return;
    }

    function commission($orderOpus){
        if($orderOpus['is_commission']==1){
            $buyer=$this->getFindByid($orderOpus['buyer_id']);
            $title='分享佣金-'.$buyer['nickname'];
            $remark='好友订单编号'.$orderOpus['ordersn'];
            if(!empty($orderOpus['member_uid'])){
                (new UserModel())->setBalance($orderOpus['member_uid'],$orderOpus['member_commission_price'],2,1,$title,$remark,$orderOpus['id']);
            }
            if(!empty($orderOpus['head_uid'])){
                (new UserModel())->setBalance($orderOpus['head_uid'],$orderOpus['head_commission_price'],2,1,$title,$remark,$orderOpus['id']);
            }
        }
    }

    public function isWriter($writer_id)
    {
        return $this->where('id',$writer_id)
            ->where('is_writer',1)->find();
    }
}